package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DSpecificationExcution;
import com.fortune.dto.in.dSpecification.DSpecificationAddDto;
import com.fortune.dto.in.dSpecification.DSpecificationEditDto;
import com.fortune.dto.in.dSpecification.DSpecificationListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DSpecificationEnum;
import com.fortune.service.DSpecificationService;
import com.fortune.vo.dSpecification.DSpecification;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "dProduct", description = "经销商商品规格型号管理", tags = {"经销商商品规格型号管理相关"})
@RestController
@RequestMapping("dSpecification")
public class DSpecificationController {
    @Autowired
    private DSpecificationService dSpecificationService;

    @ApiOperation(value = "经销商商品规格型号列表", nickname = "dSpecificationList", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int")
    })
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData<DSpecification>> list(HttpServletRequest request, @ApiIgnore @Validated DSpecificationListDto specificationListDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        specificationListDto.setPage((specificationListDto.getPage()-1)*10);
        List<DSpecification> list = dSpecificationService.queryDSpecificationList(did,specificationListDto);
        int count = dSpecificationService.queryDSpecificationListCount(did,specificationListDto);
        PagingData<DSpecification> pagingData = new PagingData<>();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new DSpecificationExcution<PagingData<DSpecification>>(pagingData, DSpecificationEnum.SUCCESS);
    }

    @ApiOperation(value = "添加商品规格型号", nickname = "dSpecificationAdd", notes = "" ,tags = {"@石匡"})
    @PostMapping(value = "add",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody DSpecificationAddDto specificationAddDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        int num = dSpecificationService.queryAddDSpecificationExist(did,specificationAddDto);
        if (num > 0){
            return new DSpecificationExcution<Void>(null, DSpecificationEnum.EXIST);
        }
        num = dSpecificationService.addDSpecification(did,specificationAddDto);
        if (num < 1){
            return new DSpecificationExcution<Void>(null, DSpecificationEnum.FAILURE);
        }
        return new DSpecificationExcution<Void>(null, DSpecificationEnum.SUCCESS);
    }

    @ApiOperation(value = "编辑商品规格型号", nickname = "dSpecificationEdit", notes = "" ,tags = {"@石匡"})
    @PutMapping(value = "edit",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DSpecificationEditDto specificationEditDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        int num = dSpecificationService.queryEditDSpecificationExist(did,specificationEditDto);
        if (num > 0){
            return new DSpecificationExcution<Void>(null, DSpecificationEnum.EXIST);
        }
        num = dSpecificationService.editDSpecification(did,specificationEditDto);
        if (num < 1){
            return new DSpecificationExcution<Void>(null, DSpecificationEnum.FAILURE);
        }
        return new DSpecificationExcution<Void>(null, DSpecificationEnum.SUCCESS);
    }

    @ApiOperation(value = "删除商品规格型号", nickname = "dSpecificationDelete", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParam(name="specificationId", value="商品规格型号id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotEmpty(message="商品规格型号id不能为空") String specificationId) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        int num = dSpecificationService.deleteDSpecification(did,specificationId);
        if (num < 1){
            return new DSpecificationExcution<Void>(null, DSpecificationEnum.FAILURE);
        }
        return new DSpecificationExcution<Void>(null, DSpecificationEnum.SUCCESS);
    }
}
